home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / picture / coord.c < prev    next >
Text File  |  1993-09-23  |  2KB  |  85 lines

  1. //    Copyright 1993 Ralph Gonzalez
  2.  
  3. /*
  4. *    FILE:        coord.c
  5. *    AUTHOR:        R. Gonzalez
  6. *    CREATED:    October 4, 1990
  7. *
  8. *    methods for 2D and 3D coordinates
  9. */
  10.  
  11. # include    "coord.h"
  12.  
  13. /******************************************************************
  14. *    initialize
  15. ******************************************************************/
  16. Coord2::Coord2(void)
  17. {
  18.     set(0.,0.);
  19. }
  20.  
  21. /******************************************************************
  22. *    set values
  23. ******************************************************************/
  24. void    Coord2::set(double x_val,double y_val)
  25. {
  26.     x = x_val;
  27.     y = y_val;
  28. }
  29.  
  30. /******************************************************************
  31. *    convert 2D coordinate from one frame into a new one
  32. ******************************************************************/
  33. void    Coord2::convert(Coord2* c,Frame* old_frame,Frame* new_frame)
  34. {
  35.     double    ratio;
  36.     
  37.     ratio = new_frame->width/old_frame->width;
  38.     x = new_frame->x + (c->x-old_frame->x) * ratio;
  39.     
  40.     ratio = new_frame->height/old_frame->height;
  41.     y = new_frame->y + (c->y-old_frame->y) * ratio;
  42. }
  43.  
  44. /******************************************************************
  45. *    initialize
  46. ******************************************************************/
  47. Coord3::Coord3(void)
  48. {
  49.     set(0.,0.,0.);
  50. }
  51.  
  52. /******************************************************************
  53. *    set values
  54. ******************************************************************/
  55. void    Coord3::set(double x_val,double y_val,double z_val)
  56. {
  57.     x = x_val;
  58.     y = y_val;
  59.     z = z_val;
  60. }
  61.  
  62. /******************************************************************
  63. *    applies the transformation to the coordinate.  This is done
  64. *    by converting the 3D point to a 4D homogeneous coordinate and
  65. *    right-multiplying it by the 4D homogeneous transformation
  66. *    matrix to produce a new point.
  67. ******************************************************************/
  68. void    Coord3::apply(Coord3* c,Transformation* t)
  69. {
  70.     x = c->x*t->m[0][0] + c->y*t->m[1][0] + c->z*t->m[2][0] + t->m[3][0];
  71.     y = c->x*t->m[0][1] + c->y*t->m[1][1] + c->z*t->m[2][1] + t->m[3][1];
  72.     z = c->x*t->m[0][2] + c->y*t->m[1][2] + c->z*t->m[2][2] + t->m[3][2];
  73. }
  74.  
  75. /******************************************************************
  76. *    set coord equal to another.
  77. ******************************************************************/
  78. void    Coord3::equate(Coord3* c)
  79. {
  80.     x = c->x;
  81.     y = c->y;
  82.     z = c->z;
  83. }
  84.  
  85.